package it.unibz.resources;

import it.unibz.User;
import java.util.*;

public class UserManager 
{
	private static List<User> userList = new ArrayList<User>();
	
	public boolean addUser(String id, String name, String status)
	{
		User usr = getUserById(id);
		if (usr == null)
		{
			userList.add(new User(id, name, status));
			return true;
		}
		else
			return false;
						
	}
	
	public boolean deleteUser(String id)
	{
		User usr = getUserById(id);
		if (usr != null)
		{
			userList.remove(usr);
			return true;
		}
		else
			return false;
	}
	
	public boolean setUserName(String id, String newName)
	{
		User usr = getUserById(id);
		if (usr != null)
		{
			usr.setName(newName);
			return true;
		}
		else
			return false;
	}
	
	public String getUserName(String id)
	{
		User usr = getUserById(id);
		if (usr != null)
		{
			return usr.getName();
		}
		else
			return "";
	}
	
	public boolean setUserStatus(String id, String newStatus)
	{		
		User usr = getUserById(id);
		if (usr != null)
		{
			usr.setStatus(newStatus);
			return true;
		}
		else
			return false;
	}
	
	public String getUserStatus(String id)
	{		
		User usr = getUserById(id);
		if (usr != null)
		{
			return usr.getStatus();
		}
		else
			return "";
	}
	
	public User getUserById(String id)
	{
		if (!userList.isEmpty())
		{
			for (int i = 0; i < userList.size(); i++)
			{
				User usr = userList.get(i);
				if (usr.getId().equals(id))
					return usr;
			}
		}
		return null;
	}
	
	public List<User> getUsers()
	{
		return userList;
	}
	
	public int getNumOfUsers()
	{
		return userList.size();
	}

}
